"""
请求和响应基类模块
"""

from jlpay.exception.exceptions import ConfigException


class OrgBaseReq:
    """机构请求基类"""
    
    def __init__(self):
        self.org_no = None
        self.merch_no = None
        self._sensitive_fields = []  # 敏感字段列表
    
    def set_org_no(self, org_no):
        """
        设置机构号
        
        Args:
            org_no: 机构号
            
        Returns:
            OrgBaseReq实例
        """
        self.org_no = org_no
        return self
    
    def set_merch_no(self, merch_no):
        """
        设置商户号
        
        Args:
            merch_no: 商户号
            
        Returns:
            OrgBaseReq实例
        """
        self.merch_no = merch_no
        return self
    
    def check_required_params(self, config):
        """
        检查必需参数
        
        Args:
            config: OrgConfig实例
            
        Raises:
            ConfigException: 参数不正确时抛出
        """
        # 已移除机构号和商户号的非空校验
        pass
    
    def add_sensitive_field(self, field_name):
        """
        添加敏感字段
        
        Args:
            field_name: 字段名
            
        Returns:
            OrgBaseReq实例
        """
        if field_name not in self._sensitive_fields:
            self._sensitive_fields.append(field_name)
        return self
    
    def get_sensitive_fields(self):
        """
        获取敏感字段列表
        
        Returns:
            敏感字段列表
        """
        return self._sensitive_fields
    
    def to_dict(self):
        """
        将请求对象转换为字典，过滤掉值为None的字段
        
        Returns:
            字典对象
        """
        return {k: v for k, v in self.__dict__.items() if not k.startswith('_') and v is not None}


class BaseResponse:
    """响应基类"""
    
    def __init__(self, response_dict=None):
        """
        初始化响应对象
        
        Args:
            response_dict: 响应字典
        """
        self.ret_code = None
        self.ret_msg = None
        self._origin_data = None
        
        if response_dict:
            self._origin_data = response_dict
            for key, value in response_dict.items():
                setattr(self, key, value)
    
    def is_success(self):
        """
        判断请求是否成功
        
        Returns:
            如果响应码为00000或00，返回True，否则返回False
        """
        return self.ret_code in ["00000", "00"]
    
    def get_origin_data(self):
        """
        获取原始响应数据
        
        Returns:
            原始响应数据字典
        """
        return self._origin_data
    
    def to_dict(self):
        """
        将响应对象转换为字典
        
        Returns:
            字典对象
        """
        data = {}
        for key, value in self.__dict__.items():
            if not key.startswith('_'):
                data[key] = value
        return data 